﻿using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Traceability.ErrorCode;
using Traceability.Write.Api.Application.Command.CommodityCommand;

namespace Traceability.Write.Api.Controllers
{
    /// <summary>
    /// 商品控制器-写
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class CommodityController : ControllerBase
    {

        private readonly IMediator mediator;

        public CommodityController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 添加商品操作
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> Handle(AddCommodityCommand request)
        {
            try
            {
                return await mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 图片上传
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> UploadImages(IFormFile file)
        {
            try
            {
                var res = "wwwroot/tu/" + file.FileName;
                using (FileStream fs = new FileStream(res, FileMode.OpenOrCreate))
                {
                    file.CopyTo(fs);
                }
                return Ok("https://localhost:7293/tu/" + file.FileName);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 修改商品操作
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult<int>> UpdateCommodity(UpdateCommodityCommand request)
        {
            try
            {
                return await mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 删除商品操作
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpDelete]
        public Task<ApiResult<int>> DelCommodity([FromQuery] DelCommodityCommand request)
        {
            try
            {
                return mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 批量删除商品操作
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ApiResult<int>> DatchDelCommodity([FromQuery] DatchDelCommodityCommand request)
        {
            try
            {
                return await mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
